package com.xfp.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xfp.anntation.AutoFill;
import com.xfp.context.BaseContext;
import com.xfp.enumclass.OperationClass;
import com.xfp.mapper.NodeMapper;
import com.xfp.mapper.RegionMapper;
import com.xfp.mapper.VmMapper;
import com.xfp.mapper.VmTypeMapper;
import com.xfp.method.pageTotal;
import com.xfp.pojo.dto.VmDto;
import com.xfp.pojo.dto.VmPolicyListDto;
import com.xfp.pojo.dto.VmQueryDto;
import com.xfp.pojo.dto.newVmDto;
import com.xfp.pojo.entity.*;
import com.xfp.pojo.vo.VmQueryVO;
import com.xfp.pojo.vo.VmVo;
import com.xfp.pojo.vo.newVmVo;
import com.xfp.service.VmService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;

@Service
@Slf4j
public class VmServiceImp implements VmService {

    @Autowired
    private VmMapper vmMapper;
    @Autowired
    private VmTypeMapper vmTypeMapper;
    @Autowired
    private RegionMapper regionMapper;
    @Autowired
    private NodeMapper nodeMapper;


    /**
     * 搜索售货机实现
     * @param dto
     * @return
     */
    @Override
    public VmQueryVO pageVm(VmQueryDto dto) {
        //开启插件
        PageHelper.startPage(dto.getPageIndex(),dto.getPageSize());
        //获取基础信息
        Page<Vm> page = vmMapper.pageVm(dto);
        List<VmVo> list = new ArrayList<>();
        //补全信息
        page.forEach(p->{
            VmVo vo = new VmVo();
            BeanUtils.copyProperties(p,vo);
            VmType vt = vmTypeMapper.findById(p.getVmType());
            vo.setType(vt);
            Region rg = regionMapper.findById(p.getRegionId());
            vo.setRegion(rg);
            Node node = nodeMapper.findById(p.getNodeId());
            Business bs = nodeMapper.findBusinessById(p.getBusinessId());
            node.setRegion(rg);
            node.setBusinessType(bs);
            vo.setNode(node);
            Status status = new Status();
            Map<Integer,Boolean> map = new HashMap<>();//TODO
            map.put(10001,true);
            map.put(10002,true);
            map.put(10003,true);
            vo.setStatus(map);
            list.add(vo);
        });
        VmQueryVO vo = new VmQueryVO();
        Long l = pageTotal.total(page.getTotal(),dto.getPageSize());
        vo.setPageIndex(dto.getPageIndex());
        vo.setPageSize(dto.getPageSize());
        vo.setTotalCount(page.getTotal());
        vo.setTotalPage(l);
        vo.setCurrentPageRecords(list);
        return vo;
    }


    /**
     * 新增售货机
     * @param dto
     * @return
     */
    @Override
    public newVmVo insertVm(newVmDto dto) {
        log.info("dto:{}",dto);
        //补全字段
        Node node = nodeMapper.findById(dto.getNodeId());
        VmType vt = vmTypeMapper.findById(dto.getVmType());
        newVmVo vo = new newVmVo();
        Vm vm = new Vm();
        BeanUtils.copyProperties(node,vo);
        BeanUtils.copyProperties(vt,vo);
        BeanUtils.copyProperties(dto,vm);
        BeanUtils.copyProperties(vt,vm);
        BeanUtils.copyProperties(node,vm);
        vm.setInnerCode(System.currentTimeMillis()+"");
        vm.setVmStatus(0);
        vm.setCityCode("0");
        vm.setLastSupplyTime(LocalDateTime.now());
        vmMapper.insert(vm);
        return vo;
    }

    /**
     * 修改设备
     * @param skuId
     */
    @Override
    public void updateVm(String skuId , String nodeId) {
        Node node = nodeMapper.findById(nodeId);
        Vm vm = new Vm();
        vm.setNode(node);
        vmMapper.updateVmNode(new TimeAndUser(),skuId,nodeId);
    }

    /**
     * 查询售货机策略
     * @param dto
     * @return
     */
    @Override
    public List<Policy> findVmPolicy(VmPolicyListDto dto) {
        List<Policy> list = vmMapper.findVmPolicy(dto);
        return list;
    }

    /**
     * 售货机策略应用
     * @param dto
     */
    @Override
    @AutoFill(OperationClass.UPDATE)
    public void applyPolicy(VmPolicyListDto dto) throws Exception {
        //查寻设备id
        List<String> innerCodeList = Arrays.asList(dto.getInnerCodeList());
        List<String> vmIdList = vmMapper.findVmIdList(innerCodeList);
        //查询折扣数
        int discount = vmMapper.finDiscount(dto.getPolicyId());
        String policyName = vmMapper.findPolicyName(dto.getPolicyId());
        //删除已有的策略
        vmMapper.deletePolicyByInnerCodeList(innerCodeList);
        vmMapper.applyPolicy(innerCodeList,"1",dto.getPolicyId(),policyName,discount, LocalDateTime.now(),LocalDateTime.now(),BaseContext.getCurrentId(),BaseContext.getCurrentId());
    }

    /**
     * 取消策略
     * @param innerCode
     */
    @Override
    public void canncelPolicy(Integer innerCode) {
        vmMapper.deleteByIdInnerCode(innerCode);
    }
}
